﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using UnityEditor;

namespace UIFramework.Editor
{
    public static class NuclearJudge
    {
        /*
         产出时的唯一核心组件判定
         根据加入列表先后顺序判定返回
             */

        //用列表的有序性来保证nuclear的唯一性
        private static List<System.Type> _types = new List<System.Type>();
        private static List<string> _typeNames = new List<string>();

        static NuclearJudge()
        {
            //先添加功能组件，因为他可能和显示组件同时存在
            AddTypes(typeof(UIButton), typeof(InputField), typeof(Slider)
                , typeof(Toggle), typeof(Scrollbar), typeof(ScrollRect)
                , typeof(Dropdown), typeof(GridLayoutGroup), typeof(HorizontalLayoutGroup)
                , typeof(VerticalLayoutGroup), typeof(CanvasGroup)
                );

            //显示组件
            AddTypes(typeof(RawImage), typeof(Image)
                , typeof(UIText), typeof(Text)
                );

            //显示组件 TMP
            AddTypeName(typeof(TMPro.TextMeshProUGUI), "TMPro.TextMeshProUGUI");
        }

        private static void AddTypes(params System.Type[] types)
        {
            for (int i = 0; i < types.Length; i++)
            {
                AddTypeName(types[i], types[i].Name);
            }
        }

        private static void AddTypeName(System.Type type,string typeName)
        {
            if (!_types.Contains(type))
            {
                _types.Add(type);
                _typeNames.Add(typeName);
            }
        }

        public static string GetNuclearTypeName(Transform trans)
        {
            UIBehaviour[] uIBehaviour = trans.GetComponents<UIBehaviour>();
            HashSet<System.Type> namelist = new HashSet<System.Type>();
            for (int i = 0; i < uIBehaviour.Length; i++)
            {
                namelist.Add(uIBehaviour[i].GetType());
            }
            //依次判断，找到直接返回
            for (int i = 0; i < _types.Count; i++)
            {
                if (namelist.Contains(_types[i]))
                {
                    return _typeNames[i];
                }
            }
            //匹配失败，返回OBJ
            return "GameObject";
        }

    }
}

